package retrofitdemo.com.night.utils;

import android.app.Activity;

import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;

import jp.wasabeef.glide.transformations.internal.Utils;
import retrofitdemo.com.night.MyApplication;
import retrofitdemo.com.night.bean.ApiService;
import retrofitdemo.com.night.bean.CommintBean;
import retrofitdemo.com.night.bean.DoWithCommintBean;
import retrofitdemo.com.night.bean.FindListCommendUpBean;
import retrofitdemo.com.night.bean.FindListUpBean;
import retrofitdemo.com.night.bean.LoginUpBean;
import retrofitdemo.com.night.bean.RegisterBean;
import retrofitdemo.com.night.bean.UpOneBean;

import static com.lzy.okgo.OkGo.get;
import static com.lzy.okgo.OkGo.post;

/**
 * Created by 86178 on 2020/3/11.
 * author mogen
 * 用心，成就一切。
 */
public class HTTPAPI {

    public static HTTPAPI instance;
    public static String token;

    private static Gson gson = null;

    public static HTTPAPI getInstance() {
        if (instance == null) {
            instance = new HTTPAPI();
        }
        if (gson == null) {
            gson = new Gson();
        }
        return instance;
    }

    /**
     * 获取博物馆楼层
     *
     * @param callback
     */
    public void getBWGFloors(StringCallback callback) {
        post(ApiService.GET_BWG_FLOORS)
                .tag(this)
                .connTimeOut(30000)
                .execute(callback);
    }

    /**
     * 获取博物馆详情
     *
     * @param callback
     */
    public void getBWGDesc(StringCallback callback) {
        post(ApiService.BWG_DESC)
                .tag(this)
                .connTimeOut(30000)
                .execute(callback);
    }


    /**
     * 获取楼层多图片
     *
     * @param id
     * @param callback
     */
    public void getFloorMoreImage(String id, StringCallback callback) {
        get(ApiService.BWG_MORE_IMG)
                .tag(this)
                .connTimeOut(30000)
                .params("lid", id)
                .execute(callback);
    }

    /**
     * 获取图片详情
     *
     * @param id
     * @param callback
     */
    public void getFloorMoreImageDesc(String id, StringCallback callback) {
        get(ApiService.BWG_IMG_DESC)
                .tag(this)
                .connTimeOut(30000)
                .params("id", id)
                .execute(callback);
    }

    /**
     * 获取博物馆活动
     *
     * @param callback
     */
    public void getBWGClass(StringCallback callback) {
        get(ApiService.FLOOR_CLASS)
                .tag(this)
                .connTimeOut(30000)
                .execute(callback);
    }


    /**
     * 登录
     *
     * @param phone
     * @param pwd
     * @param callback
     */
    public void login(String phone, String pwd, StringCallback callback) {
        LoginUpBean bean = new LoginUpBean();
        LoginUpBean.ParamsBean paramsBean = new LoginUpBean.ParamsBean();
        paramsBean.setCode(phone);
        paramsBean.setPassword(pwd);
        bean.setParamsBean(paramsBean);
        LogUtils.e("登录请求-->" + gson.toJson(bean));
        post(ApiService.URL_LOGIN)
                .tag(this)
                .connTimeOut(30000)
                .upJson(gson.toJson(bean))
                .execute(callback);
    }

    /**
     * 注册
     *
     * @param phone
     * @param name
     * @param pwd
     * @param callback
     */
    public void register(String phone, String name, String pwd, StringCallback callback) {
        RegisterBean bean = new RegisterBean();
        RegisterBean.ParamsBean paramsBean = new RegisterBean.ParamsBean();
        paramsBean.setMobile(phone);
        paramsBean.setName(name);
        paramsBean.setPassword(pwd);
        bean.setParamsBean(paramsBean);
        LogUtils.e("注册请求-->" + gson.toJson(bean));
        post(ApiService.URL_REGISTER)
                .tag(this)
                .connTimeOut(30000)
                .upJson(gson.toJson(bean))
                .execute(callback);
    }

    /**
     * 获取基本信息
     *
     * @param callback
     */
    public void getInfo(Activity activity, StringCallback callback) {
        token = (String) SPUtil.get(activity, "module_token", "");
        post(ApiService.URL_GET_INFO)
                .tag(this)
                .headers("token", token)
                .connTimeOut(30000)
                .execute(callback);
    }

    /**
     * 打卡
     *
     * @param callback
     */
    public void playCar(Activity activity, StringCallback callback) {
        token = (String) SPUtil.get(activity, "module_token", "");
        post(ApiService.URL_PLAY_CAR)
                .tag(this)
                .headers("token", token)
                .connTimeOut(30000)
                .execute(callback);
    }

    /**
     * 打卡回显
     *
     * @param time
     * @param callback
     */
    public void playCarHistory(Activity activity, String time, StringCallback callback) {
        LogUtils.e("打卡回显请求-->" + gson.toJson(new UpOneBean(time)));
        token = (String) SPUtil.get(activity, "module_token", "");
        post(ApiService.URL_PLAY_CAR_HISTORY)
                .tag(this)
                .connTimeOut(30000)
                .headers("token", token)
                .upJson(gson.toJson(new UpOneBean(time)))
                .execute(callback);
    }

    /**
     * 收藏
     *
     * @param activity
     * @param callback
     */
    public void collectHistory(Activity activity, StringCallback callback) {
        token = (String) SPUtil.get(activity, "module_token", "");
        get(ApiService.URL_FIND_MY_IMP)
                .tag(this)
                .connTimeOut(30000)
                .headers("token", token)
                .execute(callback);
    }

    /**
     * 古句查询
     *
     * @param activity
     * @param pageNo
     * @param callback
     */
    public void findList(Activity activity, String pageNo, StringCallback callback) {
        token = (String) SPUtil.get(activity, "module_token", "");
        FindListUpBean bean = new FindListUpBean();
        FindListUpBean.PageBean pageBean = new FindListUpBean.PageBean();
        pageBean.setPageNo(pageNo);
        pageBean.setPageSize("20");
        bean.setPage(pageBean);
        LogUtils.e("古句查询-->" + GsonUtil.BeanToJson(bean));
        post(ApiService.URL_FIND_LIST)
                .tag(this)
                .connTimeOut(30000)
                .headers("token", token)
                .upJson(gson.toJson(bean))
                .execute(callback);

    }

    /**
     * 获取评论
     *
     * @param activity
     * @param id
     * @param pageNo
     * @param callback
     */
    public void findListCommoned(Activity activity, String id, String pageNo, StringCallback callback) {
        token = (String) SPUtil.get(activity, "module_token", "");
        FindListCommendUpBean bean = new FindListCommendUpBean();
        FindListCommendUpBean.PageBean pageBean = new FindListCommendUpBean.PageBean();
        pageBean.setPageNo(pageNo);
        pageBean.setPageSize("5");
        bean.setParams(id);
        bean.setPage(pageBean);
        LogUtils.e("评论查询-->" + GsonUtil.BeanToJson(bean));
        post(ApiService.URL_FIND_COMMINT)
                .tag(this)
                .connTimeOut(30000)
                .headers("token", token)
                .upJson(gson.toJson(bean))
                .execute(callback);

    }

    /**
     * 1：喜欢 2：不喜欢 3：收藏
     *
     * @param id
     * @param type
     * @param callback
     */
    public void doWithCommint(Activity activity, String id, String type, StringCallback callback) {
        token = (String) SPUtil.get(activity, "module_token", "");
        DoWithCommintBean bean = new DoWithCommintBean();
        DoWithCommintBean.ParamsBean paramsBean = new DoWithCommintBean.ParamsBean();
        paramsBean.setId(id);
        paramsBean.setType(type);
        bean.setParams(paramsBean);

        LogUtils.e("评论收藏-->" + GsonUtil.BeanToJson(bean));
        post(ApiService.URL_DO_WITH_COMMINT)
                .tag(this)
                .connTimeOut(30000)
                .headers("token", token)
                .upJson(gson.toJson(bean))
                .execute(callback);
    }


    /**
     * 评论某句
     *
     * @param activity
     * @param id
     * @param comment
     * @param callback
     */
    public void commint(Activity activity, String id, String comment, StringCallback callback) {
        token = (String) SPUtil.get(activity, "module_token", "");
        CommintBean bean = new CommintBean();
        CommintBean.ParamsBean paramsBean = new CommintBean.ParamsBean();
        paramsBean.setId(id);
        paramsBean.setComment(comment);
        bean.setParams(paramsBean);
        LogUtils.e("评论-->" + GsonUtil.BeanToJson(bean));
        post(ApiService.URL_COMMINT)
                .tag(this)
                .connTimeOut(30000)
                .headers("token", token)
                .upJson(gson.toJson(bean))
                .execute(callback);
    }


}
